package com.project.dao.Impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public List<UserBean> findAll() {
        this.setConnection();
        List<UserBean> list=new ArrayList<>();
        try {
            ps=con.prepareStatement("SELECT * FROM u_user");
            rs= ps.executeQuery();
            while (rs.next()){
                list.add(this.getUser());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public UserBean login(String userName, String pwd) {
        this.setConnection();
        try {
            ps=con.prepareStatement("SELECT * FROM u_user WHERE u_userName= ? AND u_pwd= ?");
            ps.setObject(1,userName);
            ps.setObject(2,pwd);
            rs= ps.executeQuery();
            if (rs.next()){
               return this.getUser();
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
          this.closeConnection();
        }
        return null;
    }

    private UserBean getUser() {
        UserBean userBean = new UserBean();
        try {
            userBean.setId(rs.getInt("pk_userId"));
            userBean.setPwd(rs.getString("u_pwd"));
            userBean.setUserName(rs.getString("u_userName"));
            userBean.setGrade(rs.getString("u_grade"));
            userBean.setRealName(rs.getString("u_realName"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return userBean;
    }

    @Override
    public void addUser(UserBean userBean) {
        this.setConnection();
        try {
            ps=con.prepareStatement("insert into u_user(u_userName,u_realName,u_pwd,u_grade)VALUES(?,?,?,?)");
            ps.setObject(1,userBean.getUserName());
            ps.setObject(2,userBean.getRealName());
            ps.setObject(3,userBean.getPwd());
            ps.setObject(4,userBean.getGrade());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void delUser(int id) {
        this.setConnection();
        try {
            ps=con.prepareStatement("delete  from u_user where pk_userId=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public UserBean findById(int id) {
        UserBean user=null;
        this.setConnection();
        try {
            ps=con.prepareStatement("select * from u_user where pk_userId=?");
            ps.setObject(1,id);
            rs=ps.executeQuery();
            while (rs.next()){
                user=this.getUser();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return user;
    }

    @Override
    public void updateUserLevel(int id, String level) {
        this.setConnection();
        try {
            ps=con.prepareStatement("update u_user set u_grade=? where pk_userId=?");
            ps.setObject(1,level);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void updateUserPwd(int id, String pwd) {
        this.setConnection();
        try {
            ps=con.prepareStatement("UPDATE u_user SET u_pwd=? WHERE pk_userId=?");
            ps.setObject(1,pwd);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public boolean checkName(String userName) {
        UserBean user=null;
        this.setConnection();
        try {
            ps=con.prepareStatement("SELECT * FROM u_user WHERE u_userName= ?");
            ps.setObject(1,userName);
            rs=ps.executeQuery();
            while (rs.next()){
                user=this.getUser();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        if (user == null){
            return true;
        }else {
            return false;
        }
    }
}
